package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.LearnedMapper;
import com.example.programming.sys.entity.Learned;

import org.springframework.web.bind.annotation.RestController;
import com.example.programming.common.tools.BaseController;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/sys/learned")
public class LearnedController extends BaseController {
    @Autowired
    private LearnedMapper learnedMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Learned> page = new Page<Learned>(pageNum, pageSize);
        Learned parms = new Learned();
        QueryWrapper<Learned> warpper = new QueryWrapper<Learned>(parms);

        result.setData(learnedMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/sav")
    public Result save(@RequestBody Learned entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            learnedMapper.insert(entity);
        } else {
            learnedMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        learnedMapper.deleteBatchIds(deleteIds);
        return result;
    }
    @RequestMapping(method = RequestMethod.POST,value = "/save")
    public void add(String stuid ,String couid, String chapid) {
        Learned learned1 = new Learned();
        String s = UUID.randomUUID().toString().substring(0,10);
        learned1.setId(s);
        learned1.setStuid(stuid);
        learned1.setChapid(chapid);
        learned1.setCouid(couid);
        learned1.setLearned("1");
        learnedMapper.insert(learned1);

    }
    @RequestMapping(method = RequestMethod.POST, value = "/get")
    public Result get(String stuid ,String couid) {
        Result result = new Result();
        Learned parms = new Learned();
        parms.setStuid(stuid);
        parms.setCouid(couid);
        QueryWrapper<Learned> warpper = new QueryWrapper<Learned>(parms);
        result.setData(learnedMapper.selectList(warpper));
        return result;
    }

}
